<?php include "antet.php"; include "func.php";

if (isset($_SESSION["user"][0], $_GET["town"]))
{
	$_GET["town"]=clean($_GET["town"]);
	check_r($_GET["town"]);
	check_uup($_GET["town"]);
	$town=town($_GET["town"]); if ($town[1]!=$_SESSION["user"][0]) {header('Location: login.php'); die();}
	$faction=faction($_SESSION["user"][10]); $r=$faction[3];
	$buildings=buildings($_SESSION["user"][10]);
	$upq=get_up($_GET["town"]);
	$c_status=get_con($_GET["town"]);
	$units=units($faction[0]);
	$uup_status=get_uup($town[0]);

	$data=explode("-", $town[8]);
	$res=explode("-", $town[10]);
	$prod=explode("-", $town[9]);
	$lim=explode("-", $town[11]);
	$u_upgrades=explode("-", $town[17]);
	$w_upgrades=explode("-", $town[18]);
	$a_upgrades=explode("-", $town[19]);
	$s_upgrades=explode("-", $town[20]);
}
else {header('Location: login.php'); die();}
kepala($title." - ".$buildings[11][2]);
?>
<script type="text/javascript">
var res_start0=<?php echo floor($res[0]);?>; var res_start1=<?php echo floor($res[1]);?>; var res_start2=<?php echo floor($res[2]);?>; var res_start3=<?php echo floor($res[3]);?>; var res_start4=<?php echo floor($res[4]);?>;
var res_limit0=<?php echo $lim[0];?>; var res_limit1=<?php echo $lim[1];?>; var res_limit2=<?php echo $lim[1];?>; var res_limit3=<?php echo $lim[1];?>; var res_limit4=<?php echo $lim[2];?>;
var res_ph0=<?php echo ($prod[0]-$town[3]-$town[12]);?>; var res_ph1=<?php echo ($prod[1]);?>; var res_ph2=<?php echo ($prod[2]);?>; var res_ph3=<?php echo ($prod[3]);?>; var res_ph4=<?php echo ($prod[4]);?>;
var res_sec0=0; var res_sec1=0; var res_sec2=0; var res_sec3=0; var res_sec4=0;
</script>
<?php
		$to = 16;
		$output = "";
		$output .= buildingheader($to,$buildings,$data,$lim,$town);
		$output .= ads(0,"top");
		$output .= buildingstandard($town,$to,$lim, $buildings,$faction,$c_status,"good.png",$lang['Morale']);

		$cnt = "";$li = "";$no = "";$na = "";$du = "";
		if (count($upq))
		{
			//echo "Weapon forge queue:</br>";
			for ($i=0; $i<count($upq); $i++)
			{
				$cnt++;
				$li[$i] = "cancel_uup.php?town=".$_GET["town"]."&unit=".$upq[$i][1]."&tree=".$upq[$i][2];
				$no[$i] = $upq[$i][2];
				$na[$i] = $units[$upq[$i][1]][2];
				$du[$i] = $upq[$i][0];
				$ret[$i] = "academy.php?town=".$_GET["town"];
			}
		}
		$output .= showqueue($cnt,$li,$no,$na,$du,$ret);
		echo $output;
		?>
		<p><?php
		/*<table class="q_table"
			style="border-collapse: collapse; text-indent: 0; margin-left: auto; margin-right: auto; text-align: center;"
			width="600" border="1">
			<tr>
			<td><?php echo $lang['unitType'] ?></td>
			<td><?php echo $lang['hitPoints'] ?></td>
			</tr>
			*/
		if ($data[16])
		{
			//echo  showproduction($town,$lim);
			//echo $buildings[16][8]."</br></br>";
			$output = "";
			for ($i=0; $i<count($units); $i++)
			{
				$cost=explode("-", $units[$i][4]);

				if ($u_upgrades[$i]<10)
				{
					if (!$uup_status[$i])
					{
						//HP
						$hplink = showlevel($u_upgrades[$i])."</br>".img("ok.png", $lang['upgrade'])." <a class='q_linkok' href='u_upgrade.php?unit=".$units[$i][0]."&tree=17&town=".$_GET["town"]."'>".$lang['upgrade']." ".$lang['toLevel']." ".($u_upgrades[$i]+1)."</a>";

						//ATK
						$atklink = showlevel($w_upgrades[$i])."</br>".img("ok.png", $lang['upgrade'])." <a class='q_linkok' href='u_upgrade.php?unit=".$units[$i][0]."&tree=18&town=".$_GET["town"]."'>".$lang['upgrade']." ".$lang['toLevel']." ".($w_upgrades[$i]+1)."</a>";

						//DEF
						$deflink = showlevel($a_upgrades[$i])."</br>".img("ok.png", $lang['upgrade'])." <a class='q_linkok' href='u_upgrade.php?unit=".$units[$i][0]."&tree=19&town=".$_GET["town"]."'>".$lang['upgrade']." ".$lang['toLevel']." ".($a_upgrades[$i]+1)."</a>";

						//SPD
						$spdlink = showlevel($s_upgrades[$i])."</br>".img("ok.png", $lang['upgrade'])." <a class='q_linkok' href='u_upgrade.php?unit=".$units[$i][0]."&tree=20&town=".$_GET["town"]."'>".$lang['upgrade']." ".$lang['toLevel']." ".($s_upgrades[$i]+1)."</a>";
					}
					else
					{
						$hplink = $units[$i][2]." ".$lang['upgrading'];
						$spdlink = $units[$i][2]." ".$lang['upgrading'];
						$deflink = $units[$i][2]." ".$lang['upgrading'];
						$atklink = $units[$i][2]." ".$lang['upgrading'];
					}

						//HP
						$hpcost = showcost("32hp.png",$lang['hp']." : ".$lang['hitPoints'],
						floor($cost[0]*($u_upgrades[$i]+1)),
						floor($cost[1]*($u_upgrades[$i]+1)),
						floor($cost[2]*($u_upgrades[$i]+1)),
						floor($cost[3]*($u_upgrades[$i]+1)),
						floor($cost[4]*($u_upgrades[$i]+1)),
						$units[$i][9],$hplink);

						//ATK
						$atkcost = showcost("32atk.png",$lang['atk']." : ".$lang['attackpoint'],
						floor($cost[0]*($w_upgrades[$i]+1)),
						floor($cost[1]*($w_upgrades[$i]+1)),
						floor($cost[2]*($w_upgrades[$i]+1)),
						floor($cost[3]*($w_upgrades[$i]+1)),
						floor($cost[4]*($w_upgrades[$i]+1)),
						$units[$i][9],$atklink);

						//DEF
						$defcost = showcost("32def.png",$lang['def']." : ".$lang['defensepoint'],
						floor($cost[0]*($a_upgrades[$i]+1)),
						floor($cost[1]*($a_upgrades[$i]+1)),
						floor($cost[2]*($a_upgrades[$i]+1)),
						floor($cost[3]*($a_upgrades[$i]+1)),
						floor($cost[4]*($a_upgrades[$i]+1)),
						$units[$i][9],$deflink);

						//SPD
						$spdcost = showcost("32spd.png",$lang['spd']." : ".$lang['speed'],
						floor($cost[0]*($s_upgrades[$i]+1)),
						floor($cost[1]*($s_upgrades[$i]+1)),
						floor($cost[2]*($s_upgrades[$i]+1)),
						floor($cost[3]*($s_upgrades[$i]+1)),
						floor($cost[4]*($s_upgrades[$i]+1)),
						$units[$i][9],$spdlink);
				}
				else
				{
					$hplink = "";
					$spdlink = "";
					$atklink = "";
					$deflink = "";
				}
				$extra = "<table class='cute'><tr>";
				$extra .= "<td width='25%'>".$hpcost."</td>";
				$extra .= "<td width='25%'>".$atkcost."</td>";
				$extra .= "<td width='25%'>".$defcost."</td>";
				$extra .= "<td width='25%'>".$spdcost."</td>";
				$extra .= "</tr></table>";

				$output .= box("2".$i.".gif", $units[$i][2], $units[$i][10].$extra);

			}// for
			//echo "</table>";
			echo $output;

			//if (count($upq)){ echo $lang['unitUpgQueue'].":</br>";}
			//for ($i=0; $i<count($upq); $i++){ echo "[<a class='q_link' href='cancel_uup.php?town=".$_GET["town"]."&unit=".$upq[$i][1]."&tree=".$upq[$i][2]."'>x</a>] ".$units[$upq[$i][1]][2]." - <span id='".$i."'>".$upq[$i][0]."</span><script type='text/javascript'> var id=new Array(50); timer('".$i."', 'academy.php?town=".$_GET["town"]."'); </script></br>"; }
		}
		else{ echo $lang['constrBuilding'];}
		
		$output .= about(); 
		echo $output;
?>
